[Backup Script] syntax error near unexpected token `else'
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
First of all, please don't use php code tags. For some reason they don't wrap properly, and cause very annoying side scrolling. Not to mention that this is a bash script, not php. Just use regular code tags like everyone else.
Second, your code is really hard to parse. You need to break up sections with blank lines, so that each loop can be more clearly seen as a separate unit. That alone would probably help you to parse out your error.
Making sure all the keywords in the loops and constructs line up on the same column would also help. Try putting the "do/then" keywords on the same line as the for/while/if, and line up the beginning and ending keywords. i.e. for should line up with done, if with fi, case with esac, etc. Then line up all subcommands on the same column inside the construct. In short, every command at the same logical nesting level should start on the same column.
Finally, realize that the line given in the error message is the point at which the syntax parser becomes too confused to continue processing the commands. This is not always the point where the error exists. It may be that you have forgotten to close up a command somewhere further up, and it doesn't cause an error until the parser hits something further down that conflicts with it. So start walking your way backwards from the error point until you find the actual syntax error.
Now let's look at one of the code blocks near the place where you got the error:
Code:
if [ -f /home/bigworldbackup/bigworldbackuplock.log ]; then
echo "Fail" >> fail.log
if [ "2" -ge "$counter" ]
then
exit
else
mutt -s $SUBJECT -a /home/bigworldbackup/bigworldbackuplock.log $EMAIL $EMAIL2 < /home/bigworldbackup/failem$
echo -n "" > fail.log
if
else
If we apply the formatting suggestions I made above, it would look more like this:
Code:
if [ -f /home/bigworldbackup/bigworldbackuplock.log ]; then
echo "Fail" >> fail.log
if [ "2" -ge "$counter" ]; then
exit
else
mutt -s $SUBJECT -a /home/bigworldbackup/bigworldbackuplock.log $EMAIL $EMAIL2 < /home/bigworldbackup/failem$
echo -n "" > fail.log
if
else
Well, look at that. Assuming I'm properly picking the loop out of your code, it appears that your if statement is closed by another if, rather than the fi it should have been closed with.
Last edited by David the H.; 09-24-2011 at 02:17 PM.
Reason: Removed superfluous misedit
I'd put these into a function like this at the top of the script:
Code:
removefiles() {
#Use the find command to remove files of a certain type and age.
#$1 is the directory to search.
#$2 is the pattern to search
#$3 is the mtime range to match
find "$1" -name "$2" -type f -mtime "$3" -exec rm {} \+ # (or gnu find has a -delete option)
}
I'd also add a few more comments explaining the logic behind what the code is doing. You'll be happy you did a few years down the line when you review the code and wonder just what the heck you were high on when you wrote it.
if [ ${size4%%$'\t'*} -gt ${size3} ] # too many arguments here :/
[ is a command, just like any other, with the entries following it all being arguments to that command (with a closing bracket being the last argument). In the pattern "x -gt y", there are logically only 3 arguments: x, -gt, and y.
So are there three arguments? What do the variables ${size4%%$'\t'*} and ${size3} actually output? Remember that word-splitting occurs when you don't quote variables.
Try inserting set -x and set +x before and after the affected to toggle verbose output, which will help you debug what your script is doing.
But actually, in bash, arithmetic arguments are better tested with ((..)), and string arguments with [[..]].
size3=10737418240 # 10 gb space
size=`du -sb /home/bigworldbackup/backups` # get size of the folder ,where the backups are stored.
size2=`du -sb /home/bigworld` # get size of the folder to backup
size4="echo -e "${size%%$'\t'*}+${size2%%$'\t'*}" | bc" #size + size2 = size4
if [ ${size4%%$'\t'*} -gt ${size3} ] # too many arguments here :/ | if size 4 bigger than size3(10gb) than delete old backups, if not do backup
size3=10737418240 # 10 gb space
size=`du -sb /home/bigworldbackup/backups` # get size of the folder ,where the backups are stored.
size2=`du -sb /home/bigworld` # get size of the folder to backup
size4="echo -e "${size%%$'\t'*}+${size2%%$'\t'*}" | bc" #size + size2 = size4
if [ ${size4%%$'\t'*} -gt ${size3} ] # too many arguments here :/ | if size 4 bigger than size3(10gb) than delete old backups, if not do backup
Hi,
I think you are missing the command substitution. What you probably want is:
Code:
size3=10737418240 # 10 gb space
size=`du -sb /home/bigworldbackup/backups` # get size of the folder ,where the backups are stored.
size2=`du -sb /home/bigworld` # get size of the folder to backup
size4=$(echo -e "${size%%$'\t'*}+${size2%%$'\t'*}" | bc) #size + size2 = size4
if [ ${size4} -gt ${size3} ] # too many arguments here :/ | if size 4 bigger than size3(10gb) than delete old backups, if not do backup
Notice, that you do not need String manipulation for $size4 since it does not contain any tabs.
Speaking of which, how do you newbies keep getting taught the old form anyway? The new syntax has been around for ages, is defined by posix, and is supported by all the major bourne-based shells. All of the guides I've ever seen make it clear that the backticks are deprecated. So just where are you getting your scripting information?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.